package droid.frame.view.xlist;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import droid.frame.libcore.R;

/**
 * 
 * @author coffee <br>
 *         2014年3月20日下午3:10:41
 */
public class FooterLayout extends RotateLoadingLayout {

	private final ViewGroup mPullFooterLayout;
	private final ViewGroup mPullHeaderLayout;
	private final TextView mHeaderText;
	private final TextView mSubHeaderText;

	private CharSequence mPullLabel = "正在加载";

	public FooterLayout(Context context) {
		super(context);
		mPullHeaderLayout = (ViewGroup) findViewById(R.id.pull_header_layout);
		mPullFooterLayout = (ViewGroup) findViewById(R.id.pull_footer_layout);
		mHeaderText = (TextView) findViewById(R.id.pull_to_refresh_text);
		mSubHeaderText = (TextView) findViewById(R.id.pull_to_refresh_sub_text);
		mHeaderImage.setVisibility(View.INVISIBLE);
		// 通过onclick事件, 使得FooterView不触发ListView的onItemClick事件
		this.setOnClickListener(mFooterClick);
	}

	/***************** 以下是set刷新标签 ********************/
	public void setPullLabel(CharSequence pullLabel) {
		mPullLabel = pullLabel;
		if (null != mHeaderText) {
			mHeaderText.setText(mPullLabel);
		}
	}

	public void setLastUpdatedLabel(CharSequence label) {
		if (null != mSubHeaderText) {
			if (TextUtils.isEmpty(label)) {
				mSubHeaderText.setVisibility(View.INVISIBLE);
			} else {
				mSubHeaderText.setText(label);
				// Only set it to Visible if we're GONE, otherwise VISIBLE will
				// be set soon
				if (mSubHeaderText.getVisibility() != View.VISIBLE) {
					mSubHeaderText.setVisibility(View.VISIBLE);
				}
			}
		}
	}

	private FooterListener footerListener;

	public void setFooterListener(FooterListener footerListener) {
		this.footerListener = footerListener;
	}

	private View.OnClickListener mFooterClick = new View.OnClickListener() {
		@Override
		public void onClick(View v) {
			if (footerListener != null && isEnabled()) {
				setVisibility(View.VISIBLE);
				startAnimation();
				footerListener.onFooterClick();
			}
		}
	};

	public View.OnClickListener getFooterClickListener() {
		return this.mFooterClick;
	}

	public void setFooterText(int currentPage, int totalPage, boolean isAuto) {
		stopAnimation();
		setEnabled(false);// /重置该变量, 使其不可点击.
		if (totalPage <= 1) {
			mPullHeaderLayout.setVisibility(View.GONE);
			mPullFooterLayout.setVisibility(View.GONE);
		} else {
			mPullHeaderLayout.setVisibility(View.VISIBLE);
			mPullFooterLayout.setVisibility(View.VISIBLE);
			if (currentPage >= totalPage) {
				setPullLabel("数据加载完毕");
				mHeaderImage.setVisibility(View.INVISIBLE);
				// setLastUpdatedLabel(totalPage + "/" + totalPage);
				mPullHeaderLayout.setVisibility(View.GONE);
			} else {
				mHeaderImage.setVisibility(View.INVISIBLE);
				if (isAuto) {
					setPullLabel("加载中...");
					mSubHeaderText.setVisibility(View.GONE);
					mPullFooterLayout.setVisibility(View.GONE);
				} else {
					setPullLabel("单击加载更多");
				}
				// setLastUpdatedLabel(currentPage + "/" + totalPage);
				setEnabled(true);// /设置可点击
			}
		}
	}
}
